package cn.wolfcode.car.base.web.controller.system;

import cn.wolfcode.car.base.domain.Customer;
import cn.wolfcode.car.base.query.CustomerQuery;
import cn.wolfcode.car.base.service.ICustomerService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/system/customer")
public class CustomerController {

    //模板前缀
    private static final String prefix = "base/system/customer/";
    @Autowired
    private ICustomerService customerService;

    //跳转到list页面
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix+"list";
    }
    //跳转到add页面
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix+"add";
    }
    //跳转到编辑页面
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        //回显编辑的数据
        if(id!=null){
            Customer customer = customerService.selectByPrimaryKey(id);
            model.addAttribute("customer",customer);
        }
        return prefix+"edit";
    }


    //分页查询
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Customer> query(CustomerQuery qo){
         TablePageInfo<Customer> customers = customerService.query(qo);
         return customers;
    }
    //新增
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(Customer customer){
        customerService.insert(customer);
        return new AjaxResult().success();
    }
    //修改
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Customer customer){
        customerService.updateByPrimaryKey(customer);
        return new AjaxResult().success();
    }
    //删除：注意传过来的是字符串类型的ids
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        if(ids!=null){
            //业务传过来的ids是字符串，通过，隔开的；
            customerService.batchDelete(ids);
        }
        return new AjaxResult().success();
    }

}
